/*
 * OMGApp.java
 */

package omg;

import java.awt.*;
import java.awt.event.*;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class OMGApp extends SingleFrameApplication {

    public OMGApp() {

        final TrayIcon trayIcon;

        if (SystemTray.isSupported()) {

            SystemTray tray = SystemTray.getSystemTray();
            
            Image image = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/omg/resources/music_note_small.png"));
            //Image image = Toolkit.getDefaultToolkit().getImage("src/omg/resources/music_note_small.png");

            ActionListener addSongListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    AddSong add = new AddSong(null, true);
                    add.setVisible(true);
                }
            };

            ActionListener exitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
            };

            ActionListener editStoreLocationListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    EditStoreLocation edit = new EditStoreLocation(null, true);
                    edit.setVisible(true);
                }
            };

//            ActionListener addSongsListener = new ActionListener() {
//                public void actionPerformed(ActionEvent ae) {
//                    AddSongs addSongs = new AddSongs(null, true);
//                    addSongs.setVisible(true);
//                }
//            };

            ActionListener configurationListener = new ActionListener() {
                public void actionPerformed(ActionEvent ae) {
                    Configuration configuration = new Configuration();
                    configuration.setVisible(true);
                }
            };

            PopupMenu popup = new PopupMenu();
            MenuItem defaultItem = new MenuItem("Add Song");
            MenuItem editLocItem = new MenuItem("Edit Location");
            MenuItem configItem = new MenuItem("Configuration");
            MenuItem exitItem    = new MenuItem("Exit");
            //MenuItem addSongs    = new MenuItem("Add Song(s)");
            defaultItem.addActionListener(addSongListener);
            exitItem.addActionListener(exitListener);
            editLocItem.addActionListener(editStoreLocationListener);
            configItem.addActionListener(configurationListener);
            //addSongs.addActionListener(addSongsListener);
            popup.add(defaultItem);
            popup.add(configItem);
            //popup.add(addSongs);
            popup.add(editLocItem);
            popup.add(exitItem);


            trayIcon = new TrayIcon(image, "OMG", popup);
            trayIcon.setImageAutoSize(true);

            //    Depending on which Mustang build you have, you may need to uncomment
            //    out the following code to check for an AWTException when you add
            //    an image to the system tray.

                try {
                      tray.add(trayIcon);
                } catch (AWTException e) {
                    System.err.println("TrayIcon could not be added.");
                }

        } else {
            System.err.println("System tray is currently not supported.");
        }
    }

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
//        OMGApp main = new OMGApp();
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of DesktopApplication1
     */
    public static OMGApp getApplication() {
        return Application.getInstance(OMGApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(OMGApp.class, args);
    }
}